package cn.com.dhcc.turbo.service.exch.cmdata;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.dhcc.app.core.dao.CrudDao;
import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.app.core.exception.NiceException;
import cn.com.dhcc.app.core.exception.ServiceException;
import cn.com.dhcc.app.core.handler.modulehandler.WhereCondition;
import cn.com.dhcc.app.core.service.AbstractCrudService;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.core.service.Log.LOGTYPE;
import cn.com.dhcc.app.pub.core.consts.AppConst;
import cn.com.dhcc.turbo.dao.exch.CrossNetTmpCmDataDao;
import cn.com.dhcc.turbo.entity.exch.CrossNetTmpCmData;

@Service
public class CrossNetTmpCmDataService extends AbstractCrudService<CrossNetTmpCmData> {
	@SuppressWarnings("deprecation")
	private static Logger logger = Log.getExistLogger(LOGTYPE.TURBO);

	@Autowired
	private CrossNetTmpCmDataDao dao;

	@Override
	public CrudDao<CrossNetTmpCmData> getCrudDao() {
		return dao;
	}
	
	  /**
     * 中转节点需要刻盘的
     * @return
     * @throws ServiceException
     */
	public List<CrossNetTmpCmData> getCarvedDiscs() throws ServiceException {
		WhereCondition parameter = new WhereCondition();
		parameter.where().eq("SEND_STATUS", AppConst.SendStatus.FAIL.value())
		.and().eq("CROSS_NET", AppConst.CrossNetType.OUT_INNER_NETWORK.value())
		.and().eq("THROUGH_GAP", "2");
		return this.list(parameter);
	}

	
	/**
	 * 获取本节点待发送到网闸的数据
	 * @return
	 * @throws NiceException
	 * @throws ServiceException
	 */
	public List<CrossNetTmpCmData> getLocaleGapUnsentList() throws NiceException,ServiceException {
		WhereCondition parameter = new WhereCondition();
		parameter.where().notEq("MSG_TYPE", AppConst.MsgType.GROUP_MSG.value())
			.and().eq("SEND_STATUS", AppConst.SendStatus.OUTER_ENTREPOT_READY.value());
		return this.list(parameter);
	}

	/**
	 * 获取本节点待发送的数据
	 * @return
	 * @throws NiceException
	 * @throws ServiceException
	 */
	public List<CrossNetTmpCmData> getLocaleUnsentList() throws NiceException,ServiceException {
		WhereCondition parameter = new WhereCondition();
		parameter.where().notEq("MSG_TYPE", AppConst.MsgType.GROUP_MSG.value())
			.and().eq("SEND_STATUS", AppConst.SendStatus.READY.value())
			.and().eq("CROSS_NODE", AppConst.NO)
			.and().eq("CROSS_NET", AppConst.NO);
		return this.list(parameter);
	}
	
	
	/**
	 * 获取内网网闸节点待发送的数据
	 * @return
	 * @throws NiceException
	 * @throws ServiceException
	 */
	public List<CrossNetTmpCmData> getInnerEntrepotUnsentList() throws NiceException,ServiceException {
		WhereCondition parameter = new WhereCondition();
		parameter.where().notEq("MSG_TYPE", AppConst.MsgType.GROUP_MSG.value())
			.and().eq("SEND_STATUS", AppConst.SendStatus.INNER_ENTREPOT_READY.value())
			.and().eq("CROSS_NET", AppConst.CrossNetType.OUT_INNER_NETWORK.value());
		return this.list(parameter);
	}

	/**
	 * 获取跨节点未发送的数据列表
	 * @return
	 * @throws NiceException
	 * @throws ServiceException
	 */
	public List<CrossNetTmpCmData> getCrossNodeUnsentList() throws NiceException,ServiceException {
		WhereCondition parameter = new WhereCondition();
		parameter.where().notEq("MSG_TYPE", AppConst.MsgType.GROUP_MSG.value())
			.and().eq("SEND_STATUS", AppConst.SendStatus.READY.value())
			.and().eq("CROSS_NODE", AppConst.YES)
			.and().eq("CROSS_NET", AppConst.NO);
		return this.list(parameter);
	}

	/**
	 * 获取尚未通知的数据列表
	 * 
	 * @return
	 * @throws NiceException
	 * @throws ServiceException
	 */
	public List<CrossNetTmpCmData> getToBeNotifyList() throws NiceException,
			ServiceException {
		WhereCondition parameter = new WhereCondition();
		parameter.where().eq("notify_status", AppConst.NotifyStatus.READY.value())
				.and().leftBracket()
				.eq("send_status", AppConst.SendStatus.SENT.value()).or()
				.eq("send_status", AppConst.SendStatus.FAIL.value())
				.rightBracket();
		return this.list(parameter);
	}

	/**
	 * 批量更新通知状态
	 * 
	 * @param dataList
	 * @param status
	 * @return
	 * @throws NiceException
	 */
	public int updateNotifyStatus(List<CrossNetTmpCmData> dataList, String status)
			throws NiceException {
		try {
			return dao.updateNotifyStatus(dataList, status);
		} catch (DaoException e) {
			logger.error(e);
			throw new NiceException(e);
		}
	}

	/**
	 * 批量更新数据发送状态
	 * 
	 * @param dataList
	 */
	public int updateSendStatus(List<CrossNetTmpCmData> dataList, String status)
			throws NiceException {
		try {
			return dao.updateSendStatus(dataList, status);
		} catch (DaoException e) {
			logger.error(e);
			throw new NiceException(e);
		}
	}

	/**
	 * 获取发送节点列表
	 * 
	 * @return
	 */
	public List<Map<String, Object>> getSendNodeList() throws NiceException {
		try {
			return dao.getSendNodeList();
		} catch (DaoException e) {
			logger.error(e);
			throw new NiceException(e);
		}
	}

	/**
	 * 获取接收节点列表
	 * 
	 * @return
	 */
	public List<Map<String, Object>> getRecvNodeList() throws NiceException {
		try {
			return dao.getRecvNodeList();
		} catch (DaoException e) {
			logger.error(e);
			throw new NiceException(e);
		}
	}

	
	
	
	/**
	 * 计算子消息的数目
	 * @param id
	 * @return
	 * @throws NiceException
	 * @throws ServiceException
	 */
	public int countSubMsg(String id) throws NiceException, ServiceException{
		WhereCondition searchData = new WhereCondition();
		searchData.where().eq("pid", id);
		return this.count(searchData);
	}

	/**
	 * 获取发送适配器列表
	 * 
	 * @return
	 */
	public List<Map<String, Object>> getSendAdapterList() throws NiceException {
		try {
			return dao.getSendAdapterList();
		} catch (DaoException e) {
			logger.error(e);
			throw new NiceException(e);
		}
	}
}
